package com.asj.service.command.specific;

import static com.asj.enums.DeviceIdentifierDictEnums.deviceRecovery;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_code;
import static com.asj.enums.HttpRequestDictEnums.receiveRecoveryUrl;
import static com.asj.enums.ResultCodeEnums.request_error;
import static com.asj.enums.ResultCodeEnums.result_error;
import static com.asj.enums.ResultCodeEnums.result_success;

import android.content.Context;
import android.util.Log;

import com.asj.MainActivity;
import com.asj.service.oppoSdk.OPPOControlService;
import com.asj.utils.CommonsUtils;
import com.asj.utils.HttpRequestUtils;
import com.asj.utils.JSONParamPackingUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * 恢复出厂设置
 * */
public class SpecificDeviceRecoveryService {
    public static void deviceRecovery(JsonObject contentData, Context context){
        //返回给mdm设置结果
        JsonObject lastedCommandDetail = null;
        //将请求发送回去
        String lastedCommand = HttpRequestUtils.retryHttpRequest(receiveRecoveryUrl.getValue(), contentData.toString(), result_success.getCode(), 3,context);
        Log.i("恢复出厂设置第一阶段信息：已返回结果，告知成功！",lastedCommand);
        lastedCommandDetail = JsonParser.parseString(lastedCommand).getAsJsonObject();
        try{
            if(lastedCommandDetail != null ){
                if(lastedCommandDetail.get(json_key_code.getValue()).getAsInt()  == result_success.getCode()){
                    /* 设备恢复出厂设置 */
                    new OPPOControlService(context).setRecovery();
                }
            }
        }catch (Exception e){
            JSONParamPackingUtils.functionPacking(contentData,result_error,e.getMessage());
            new Thread(()->{
                //将请求发送回去
                String result = HttpRequestUtils.retryHttpRequest(receiveRecoveryUrl.getValue(), contentData.toString(), result_success.getCode(), 3,context);
                if(!result.equals(request_error.getMessage())){
                    Log.i("恢复出厂设置失败！已返回结果，告知成功！",result);
                }
            }).start();
        }
    }
}
